//
//  GFUIRefreshControl.swift
//  我的微博练习
//
//  Created by zhangguo on 15/10/15.
//  Copyright © 2015年 zhangguo. All rights reserved.
//

import UIKit
private let kRefreshPullOffset: CGFloat = -60

//负责加载控件
class GFUIRefreshControl: UIRefreshControl {

    //重写系统结束刷新方法 写这里是为了
    override func endRefreshing() {
        super.endRefreshing()
        refrenctView.stoptLoding()
    }
    //kvo
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        //在这里设置下拉箭头的旋转
        if frame.origin.y > 0 {
            return
        }
        
        //判断是否开始开始刷新，开始就播放动画
        if refreshing {
            refrenctView.startLoding()
            
        }

        if frame.origin.y < kRefreshPullOffset && !refrenctView.rotateFlag{
            print("翻转")
            refrenctView.rotateFlag = true
            
        }else if frame.origin.y > kRefreshPullOffset && refrenctView.rotateFlag{
            
            print("转过去")
            refrenctView.rotateFlag = false
        }

    }
    
    override init() {
        super.init()
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //移除kvo
    deinit {
        self.removeObserver(self, forKeyPath: "frame")
    }
    private func setupUI()
    {
        //kvo监听frame变化，首选kvo
        self.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
        
        //添加刷新视图
        addSubview(refrenctView)
        tintColor = UIColor.clearColor()
         //自动布局,让提示文字居中，局部小时图用 xib加载比较方便
        refrenctView.ff_AlignVertical(type: ff_AlignType.CenterCenter, referView: self, size: refrenctView.bounds.size)
       }
    //懒加载 控件
    private lazy var refrenctView: GFUIRefreshView = GFUIRefreshView.refrenceView()
}


//这是负责加载动画，从xib
class GFUIRefreshView: UIView {
    class func refrenceView() -> GFUIRefreshView {
        
        return NSBundle.mainBundle().loadNibNamed("GFUIRefreshView", owner: nil, options: nil).last as! GFUIRefreshView
    }
    
    //翻转标记
    private var rotateFlag = false {
        didSet{
            roateTipeIcon()
        }
    }
    
    @IBOutlet weak var lodingIcon: UIView!

    @IBOutlet weak var tipView: UIView!
    @IBOutlet weak var tipIcon: UIImageView!
    
    private func roateTipeIcon() {
        
        let angel = rotateFlag ? CGFloat(M_PI - 0.01) : CGFloat(-M_PI + 0.01)
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.tipIcon.transform = CGAffineTransformRotate(self.tipIcon.transform, angel)
        }
    }
    
    //开始播放动画部分
    private func startLoding() {
        //0.判断动画是否已经被添加,可以防止下拉提示圈一直加速转，防止反复调用动画
        if lodingIcon.layer.animationForKey("loadingAnim") != nil {
            return
        }
        
        //隐藏提示图
        tipView.hidden = true
        
        //定义动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue  = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 1.0
        
        //添加动画
        lodingIcon.layer.addAnimation(anim, forKey: "loadingAnim")
        
    }
    
    //停止播放动画部分
    private func stoptLoding() {
        
    tipView.hidden = false
    lodingIcon.layer.removeAllAnimations()
    }
    
}